package com.lxk.service.impl;

import com.lxk.dao.AsMyMapper;
import com.lxk.dao.ContactMapper;
import com.lxk.pojo.Contact;
import com.lxk.pojo.ContactExample;
import com.lxk.service.ContactSevice;
import com.lxk.vo.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author JohnSoybean
 * @create 2020/4/9 10:09
 * @description
 */
@Service
@Transactional
public class ContactServiceImpl implements ContactSevice {

    @Autowired
    private AsMyMapper mm;
    @Autowired
    private ContactMapper cm;

    public Integer getContactPages(Integer num) {
        ContactExample example = new ContactExample();
        Integer count = (int)cm.countByExample(example);
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<Contact> selContactPage(Integer pageIndex, Integer num, Integer totalPages, Integer sort, Integer order) {
        int start = (pageIndex-1)*num;
        List<Contact> list = mm.selContactPage(start, num, sort, order);
        PageModel<Contact> model = new PageModel<Contact>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public Contact selContactById(Integer contactId) {
        Contact contact = cm.selectByPrimaryKey(contactId);
        return contact;
    }

    public void markContactHandled(Integer contactId) {
        Contact contact = new Contact();
        contact.setContactId(contactId);
        contact.setStatus(1);
        cm.updateByPrimaryKeySelective(contact);
    }
}
